package flatlandRPG;

import org.lwjgl.input.Keyboard;
import org.lwjgl.util.vector.Vector2f;

// -------------------------------------------------------------------------
/**
 * An actor that the player can talk to.
 *
 * @author evant
 * @version Apr 29, 2011
 */
public class Actor
    extends GLRegularPolygon
    implements ILogic
{
    private String[] text;
    private int      index;
    private boolean  isDisplaying;
    private boolean  keyPressed;
    private GameText gameText;


    // ----------------------------------------------------------
    /**
     * Create a new Actor object.
     *
     * @param radius
     *            the radius of the actor
     * @param sides
     *            the number of sides of the actor
     * @param gameText
     *            the gameText to show the conversation on
     * @param text
     *            the text that the actor says
     */
    public Actor(float radius, int sides, GameText gameText, String... text)
    {
        super(radius, sides);
        this.text = text;
        this.gameText = gameText;
        index = -1;
        isDisplaying = false;
    }


    // ----------------------------------------------------------
    /**
     * Tries to talk to the actor. It only happens if the location provided is
     * within the range of the Actor.
     *
     * @param location
     *            the location to try to talk to the Actor from
     */
    public void talk(Vector2f location)
    {
        if (VectorUtil.distance(location, getLocation()) < 0.5f)
        {
            gameText.setVisible(true);
            isDisplaying = true;
        }
    }


    // ----------------------------------------------------------
    /**
     * Returns whether or not the player is conversing with the Actor.
     *
     * @return true if there is a conversation, false otherwise
     */
    public boolean isConversing()
    {
        return isDisplaying;
    }


    @Override
    public void doLogic()
    {
        if (keyPressed && !TestableKeyboard.isKeyDown(Keyboard.KEY_E))
        {
            keyPressed = false;
        }

        if (isDisplaying && TestableKeyboard.isKeyDown(Keyboard.KEY_E)
            && !keyPressed)
        {
            index++;
            keyPressed = true;
            if (index >= text.length)
            {
                index = -1;
                isDisplaying = false;
                gameText.setVisible(false);
                keyPressed = false;
            }
        }
    }


    public void draw()
    {
        super.draw();

        if (isDisplaying && getText() != null)
        {
            gameText.setFormatedText(getText());
        }
        else if (isDisplaying && getText() == null)
        {
            gameText.setVisible(false);
        }
    }


    // ----------------------------------------------------------
    /**
     * Returns the text to be displayed.
     *
     * @return the text.
     */
    public String getText()
    {
        if (index >= 0)
        {
            return text[index];
        }
        else
        {
            return null;
        }
    }
}
